package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.OrderForm;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.OrderFormMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IOrderFormService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IOrderFormServiceImpl implements IOrderFormService {

    @Autowired
    private OrderFormMapper orderFormMapper ;

    @Override
    public void saveOrUpdate(OrderForm orderForm) {
        if (orderForm.getId()==null) {
            //自动保存当前登录的用户
            orderFormMapper.insert(orderForm);
            //获取当前登录用户的id
            Long userId = UserContext.getCurrentUser().getId();
            orderFormMapper.insertUserAndOrderForm(userId,orderForm.getId());

        }else {
            //自动保存当前登录的用户
            orderFormMapper.updateByPrimaryKey(orderForm);

        }

    }

    @Override
    public PageInfo<OrderForm> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<OrderForm> list = orderFormMapper.selectForList(qo);
        return new PageInfo(list);
    }
    //修改订单状态
    @Override
    public void updateStatus(Long id, Integer state) {

        orderFormMapper.updateStatus(id,state);
    }

    //根据当前登录的用户查询订单信息
    @Override
    public List<OrderForm> getUserIdOrderForm(Long userId) {
        return orderFormMapper.selectByPrimaryKey(userId);
    }

}
